import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

let refreshTimer = null
const refreshInterval = 1000 * 60 * 30

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function initTokenRefresher(currentUserStore) {
  if (!refreshTimer) {
    refreshTimer = setInterval(() => {
      currentUserStore.dispatch('RefreshToken')
    }, refreshInterval)
  }
}

export function removeTokenRefresher() {
  if (refreshTimer) {
    clearInterval(refreshTimer)
  }
}
